package design.mode.effective_java.chapter2.item6;

import java.util.regex.Pattern;

/**
 * @author ZhongMingde on 2021/8/3
 */

public class Item6 {

    /**
     * 正则 确定字符串是否为有效的罗马数字；并不可取，String在内部创建了一个Pattern实例，创建这个实例很昂贵
     *
     * @param s
     * @return
     */
    static boolean isRomanNumeral(String s) {
        return s.matches("^(?=.)M*(C[MD]|D?C{0,3})" + "(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$");
    }

    private static final Pattern ROMAN = Pattern.compile("^(?=.)M*(C[MD]|D?C{0,3})" + "(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$");

    static boolean isRomanNumeral2(String s) {
        return ROMAN.matcher(s).matches();
    }

}
